package com.lifesting.workflow.runtime.service.impl;


import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import com.lifesting.workflow.model.gd.DocumentRoot;
import com.lifesting.workflow.runtime.service.WfsService;

public class WfsServiceImpl implements WfsService {

	@Override
	public boolean run(String localFilePath) throws Exception {
		URI uri = URI.createFileURI(localFilePath);
		return run(uri);
	}
	private boolean run(URI uri) throws Exception{
		ResourceSet resourceSet = new ResourceSetImpl();
		Resource resource = resourceSet.getResource(uri,true);
		Object obj = resource.getContents().get(0);
		if (!(obj instanceof DocumentRoot)){
			throw new RuntimeException(uri+" is not a valid workflow model");
		}
		com.lifesting.workflow.model.gd.Process process = ((DocumentRoot)obj).getProcess();
		System.out.println("Execute process "+process);
		return true;
	}
	@Override
	public boolean runPluginLoaded(String pluginFilePath) throws Exception {
		URI uri = URI.createPlatformPluginURI(pluginFilePath, false);
		return run(uri);
	}

}
